let app = getApp();
import util from "../../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    powerChecked:false,
    showSearch: false,
    nickname: '',
    form:{
      cstore_id:null,
      uid:null, //会员ID
      mobile:'',
      name:'',
      owner_auth:0,
      status:0
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.storeId??'') !== '') {
      this.setData({
        'form.cstore_id':Number(options.storeId)
      })
      console.log(this.data.form.cstore_id);
    } 
    if((options.itemId??'') !== '') {
      this.setData({
        'form.id':Number(options.itemId)
      })
      this.verifierDetail();
    } 
  },
  searchTransfer(val) {
    var reSearch = val.detail.replace(/\D/g, '');
    // 防抖
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.dividendSearch(reSearch));
    }, 500);
    
    
  },
  dividendSearch(reSearch) {
    let urlStr = "";
      urlStr = app.getNetAddresss("plugin.love.Frontend.Controllers.team-dividend-search.index");
      urlStr += "&search=" + reSearch;
    if (reSearch) {
      
      this.setData({
        nickname:''
      });
      app._getNetWork({
        url: urlStr,
        success: (res) => {
          let data = res.data.data;
          if (res.data.result === 1) {
            this.setData({
              nickname: data[0].nickname,
              showSearch: true
            });
          } else {
            this.setData({
              nickname: '',
              showSearch: true
            });
            // app.tips(res.data.msg);
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  verifierChange({detail }) {
      // 需要手动对 checked 状态进行更新
    this.setData({ 
      checked: detail,
      'form.status': detail?1:0
    });
  },
  powerChange({detail }){
    this.setData({ 
      powerChecked: detail,
      'form.owner_auth': detail?1:0
    });
  },
  modelInput(event) {
    const item = event.currentTarget.dataset.item,
          itemValue = event.detail.value;
    this.setData({
      [item]:itemValue
    })
  },
  verifierDetail() {
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.clerkDetail");
    let json = {
      cstore_id: this.data.form.cstore_id,
      id: this.data.form.id
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          let query = this.data.form;
          ({  //解构赋值
            uid: query.uid,
            mobile: query.mobile,
            name: query.name,
            owner_auth: query.owner_auth,
            status: query.status,
          } = res.data);
          this.setData({
            form:query,
            checked: query.status===1?true:false,
            powerChecked: query.owner_auth===1?true:false
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  verifierBtn() {
    if(this.data.form.id) {
      if (app._isTextEmpty(this.data.nickname) && this.data.showSearch) { //修改判断是否有会员
        wx.showToast({
          icon: 'none',
          title: '未找到会员',
          duration: 1500
        });
        return;
      }
    } else {
      if (app._isTextEmpty(this.data.nickname) && app._isTextEmpty(this.data.form.id) ) { //判断是否有会员
        wx.showToast({
          icon: 'none',
          title: '未找到会员',
          duration: 1500
        });
        return;
      }
    }
    let urlStr = app.getNetAddresss("plugin.coupon-store.frontend.clerk-manage.changeClerk");
    app._postNetWork({
      url: urlStr,
      data:this.data.form,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
          //获取页面栈
          setTimeout(() => {
            wx.navigateBack({
              dalta:2
            })
          },500);

        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})